package com.csust.reflection.qusetion;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.classfile.Interfaces;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/*
*
//getName：获取全类名
//getSimpLeName：获取简单类名
//getFields：获取所有public修饰的属性，包含本类以及父类的
//getDecLaredFields：获取本类中所有属性
//getMethods：获取所有pubLic修饰的方法，包含本类以及父类的
//getDeclaredMethods：获取本类中所有方法
//getConstructors：获取所有pubLic修饰的构造器，包含本类以及父类的
//getDeclaredConstructors：获取本类中所有构造器
//getPackage：以Package形式返回 包信息
//getSuperclass：以class形式返回父类信息
//getInterfaces：以class[形式返回接口信息
//getAnnotations：以Annotation[] 形式返回注解信
*
*
*
*
*
*
*
* */


public class ReflectionUtils {
    public static void main(String[] args) {
    }
    @Test
    public void test1() {
        //第一组 API
        Class clazz = Person.class;
        //获取全类名
        System.out.println(clazz.getName());
        //获取简单类名
        System.out.println(clazz.getSimpleName());

    }
    @Test
    public void test2Fields() {
        Class clazz = Person.class;
        //无法爆破，忽略修饰符只能获得 public修饰的 属性(包括父类的属性)
        Field [] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field.getName());
        }
//        获得所有属性（包括父类）
        Field [] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println("属性="+field.getName()+"属性值="+field.getModifiers());
        }


    }
    @Test
    public void test3Methods() {
        Class clazz = Person.class;
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println(method.getName());

        }
    }
    @Test
    public void test4Constructors() {
        Class clazz = Person.class;
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor.getName());
        }
        Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
        for (Constructor constructor : declaredConstructors) {
            System.out.println(constructor.getName());
        }
    }
    @Test
    public void test5Package() {
        Class clazz = Person.class;
        Package pkg = clazz.getPackage();
        System.out.println(pkg.getName());
    }
    @Test
    public void test6SuperClass() {
        Class clazz = Person.class;
        System.out.println(clazz.getSuperclass());
    }
    @Test
    public void test7Interfaces() {
        Class clazz = Person.class;
        Class[] interfaces = clazz.getInterfaces();
//        System.out.println(clazz.getInterfaces());
        for (Class anInterface : interfaces) {
            System.out.println(anInterface.getName());
        }
    }
    @Test
    public void test8Annotations() {
        Class clazz = Person.class;
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation.annotationType().getName());
        }
    }

}
interface A{

}
interface B {}

@Deprecated
class Person implements A,B{
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
